package com.ljh.seckill.utils;

import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author grung
 * @Date 2021/2/24 9:52
 * @Version 1.0
 */

public class LockFactory {

    private RedisTemplate redisTemplate;

    public LockFactory(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public static ConcurrentHashMap<String, RedisLock> cacheLockMap = new ConcurrentHashMap(64);

    /**
     * @param name
     * @return
     */
    public RedisLock getLock(String name) {
        RedisLock redisLock = cacheLockMap.get(name);
        if (null != redisLock) {
            return redisLock;
        }
        return cacheLockMap.computeIfAbsent(name, val -> new RedisLock(redisTemplate, name));
    }
}
